package com.xxxx.server;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.xmlbeans.XmlException;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: yeb
 * @description: test
 * @author: Honors
 * @create: 2021-08-17 17:10
 */
public class test2 {
    @Test
    public void s3() {

        String str = "F:\\zhjx\\work\\workspaces\\zhjx-dev\\core\\wethink-web\\target\\wethink\\resource\\instructor\\ssdadaawdas.doc";
        String str2 = "/resource/instructor/1631167424198.jpg";
        // 获得第一个 / 的下标
        int index = str.indexOf("instructor\\");

//        System.out.println("第一个：" + index);
//        // 根据第一个点的位置 获得第二个点的位置
//        index = str.indexOf("\\", index + 10);
//        //根据第二个点的位置，截取 字符串。得到结果 result
//        index = index + 1;
        String result = str.substring(index);

        //输出结果
        System.out.println(result);
        System.out.println(str2.substring(str2.indexOf("instructor" + File.separator) + 11));
        System.out.println(str2.substring(str2.indexOf("instructor/") + 11));

    }


    @Test
    public void s2() {
        List<String> pathList = new ArrayList<>();
        pathList.add("F:\\zhjx\\work\\workspaces\\zhjx-dev\\core\\wethink-web\\target\\wethink\\resource\\instructor\\" +
                "1630912309261.png");
//        pathList.add("D:\\2.txt");
//        pathList.add("D:\\3.txt");

        // 批量删除附件
        for (String path : pathList) {
            // 找到文件
            File file = new File(path);
            // 文件是否存在
            boolean isOK = file.exists();
            // 存在就删除
            if (isOK) {
                file.delete();
                System.out.println(path + "删除成功！");
            } else {
                System.out.println("文件不存在");
            }
        }
    }

    @Test
    public void s1() {

        List<String> stu1 = new ArrayList<>();
        stu1.add("s1");
        stu1.add("s2");
        stu1.add("s3");


        List<String> scores = new ArrayList<>();
        scores.add("50");
        scores.add("60");
        scores.add("70");

        int x = 0;
        for (String s : stu1) {
            System.out.println(s);
            System.out.println("score:" + scores.get(x));
            x = x + 1;
            if (!scores.get(x).equals("50")) {
                break;
            }
        }
    }


    /**
     * 设置表格 - 属性
     *
     * @param table     表格
     * @param width     宽度
     * @param enumValue 格式
     */
    public void setTableWidthAndHAlign(XWPFTable table, String width,
            STJc.Enum enumValue) {
        CTTblPr tblPr = getTableCTTblPr(table);
        // 表格宽度
        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
        if (enumValue != null) {
            CTJc cTJc = tblPr.addNewJc();
            cTJc.setVal(enumValue);
        }
        // 设置宽度
        tblWidth.setW(new BigInteger(width));
        tblWidth.setType(STTblWidth.DXA);
    }

    /**
     * 获取表格属性
     *
     * @param table 表格
     * @return CTTblPr
     */
    private CTTblPr getTableCTTblPr(XWPFTable table) {
        CTTbl ctTbl = table.getCTTbl();
        // 表格属性
        return ctTbl.getTblPr() == null ? ctTbl.addNewTblPr() : ctTbl.getTblPr();
    }


    @Test
    public void test111() throws IOException {
        List<String> columns = new ArrayList<String>();
        columns.add("姓名");
        columns.add("性别");
        columns.add("身份证号码");
        List<String> content = new ArrayList<String>();
        content.add("1");
        content.add("2");
        content.add("3");
        downloadExcelColumn(columns, content);
    }

    public void downloadExcelColumn(List<String> columns, List<String> content) throws IOException {
        // 创建Excel文件(Workbook)
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        // 创建工作表(Sheet)
        HSSFSheet sheet = hssfWorkbook.createSheet("档案");

        // x = 行数
        int x = 0;
        HSSFRow row1 = sheet.createRow(x);
        // 遍历标题集合，设置标题
        for (int i = 0; i < columns.size(); i++) {
            // 创建第 X 行 第 I 列，设置该列的值为  columns.get(i)
            row1.createCell(i).setCellValue(columns.get(i));
        }
        // 行数 + 1
        x = x + 1;
        writeBody(sheet, content, x);

        // 输出Excel文件
        FileOutputStream output = new FileOutputStream("D:\\dangan.xls");
        hssfWorkbook.write(output);
        output.flush();
    }

    /**
     * 处理内容数据
     *
     * @param sheet
     * @param content
     * @param x
     */
    private void writeBody(HSSFSheet sheet, List<String> content, int x) {
        HSSFRow row2 = sheet.createRow(x);
        // 第二行，标题下数据内容
        for (int i = 0; i < content.size(); i++) {
            // 创建第 X 行 第 I 列，设置该列的值为  columns.get(i)
            row2.createCell(i).setCellValue(content.get(i));
        }
    }

    @Test
    public void test1() {
//        Tue Aug 10 11:04:34 CST 2021~Fri Dec 31 23:59:59 CST 9999
        Date date = new Date("Tue Aug 10 11:04:34 CST 2021");
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        System.out.println(format);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
        sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(sdf.format(date));

    }

    /**
     * 两个日期之间 过了多少天
     *
     * @throws Exception
     */
    @Test
    public void getInterval() throws Exception {
        String inTime = "2020-6-20 11:30:00";//入住时间
        String outTime = "9999-7-25 15:40:00";//退房时间

        //1.先算 年月日 的天数
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");//年月日
        Date inDate = sdf1.parse(inTime);//获取入住时间的年月日
        Date outDate = sdf1.parse(outTime);//获取退房时间的年月日
        long day = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (inDate != null) {
            String begin = sdf.format(inDate);
            inDate = sdf.parse(begin);
        }
        if (outDate != null) {
            String end = sdf.format(outDate);
            outDate = sdf.parse(end);
        }
        day = (outDate.getTime() - inDate.getTime()) / (24 * 60 * 60 * 1000);
        System.out.println(day);
    }

    @Test
    public void t2() {
        String s = "/resource/skill-identification/1631692278100.docx";
        Boolean value = s.indexOf("${TWO}") != -1;
        String url = s.substring(s.indexOf("${skill-identification/}") + 21);

        System.out.println(url);
    }

    @Test
    public void main() throws XmlException, OpenXML4JException, IOException {
        String path = "F:\\桌面文档\\汇思\\中核检修\\01功能文件\\01职业技能认证占位符文件\\" +
                "职业技能等级证书合规审核表.docx";
        // 获取文档
        OPCPackage opcPackage = POIXMLDocument.openPackage(path);
        POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
        // 获取文档所有文本
        String result = extractor.getText();

        List<String> list = new ArrayList<>();
        list.add("${SPACE1}");
        list.add("${SPACE2}");
        list.add("${SPACE3}");
        list.add("${SPACE4}");
        list.add("${SPACE5}");
        list.add("${SPACE6}");
        list.add("${SPACE7}");
        for (String text : list) {
            boolean space = result.contains(text);
            if (space) {
                System.out.println("包含");
            } else {
                System.out.println("不包含");
            }
        }
        extractor.close();
    }
}
